<template>
  <div>
    <el-tree
      ref="refTree"
      :data="list"
      :props="{ label: 'name' }"
      default-expand-all
      show-checkbox
      check-strictly
      node-key="id"
    />
    <el-button type="primary" @click="hsave">确认</el-button>
    <el-button type="success" @click="hoff">取消</el-button>
  </div>
</template>

<script>
import { getPermissionList } from '@/api/permisson.js'
import { getRoleDetail, assignPerm } from '@/api/setting.js'
import { toTree } from '@/utils/tranListToTreeData.js'
export default {
  name: 'Assign',
  props: {
    id: { type: String,
      required: true }
  },
  data() {
    return {
      list: []
    }
  },
  created() {
    this.loadGtypermission()
  },
  methods: {
    // 点击确认保存
    async hsave() {
      const permIds = this.$refs.refTree.getCheckedKeys()
      console.log(permIds)
      await assignPerm({ id: this.id, permIds })
      this.$emit('close')
      this.$message('更新权限成功')
    },
    hoff() {
      this.$emit('close')
    },
    // 获取所有权限详情
    async loadGtypermission() {
      const res = await getPermissionList()
      this.list = toTree(res.data)
    },
    // 根据id获取权限
    async loadgetPermissionDetail() {
      console.log(this.id)
      const res = await getRoleDetail(this.id)
      console.log(res)
      // 回填
      this.$refs.refTree.setCheckedKeys(res.data.permIds)
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
